# from flask_restplus import Api, Resource, fields
from flask_restful import Api
from flask import Blueprint, request, jsonify

blueprint = Blueprint("demo_crud", __name__, url_prefix="/api/public")

from sqlalchemy import and_

from .models import OkDataLevel3, OkDataLevel4, TbPhoneSegmentInfo


# api = Api(blueprint)
# ns = api.namespace("demo_crud", description="用户")
# from flask_restful import Resource


# class HelloWorldCrud(Resource):
#     def get(self, todo_id):
#         return {"name": "alex"}
#
#     def put(self, todo_id):
#         return {}


@blueprint.route("/get_location", methods=["GET"])
def get_location():
    params_dict = request.args
    t_name = params_dict.get("name", "").strip()
    res_dict = {
        "code": 200,
        "msg": "success"
    }
    if not t_name:
        res_dict["code"] = 500
        res_dict["msg"] = "参数name不能为空"

    res_object_list = OkDataLevel3.query.filter(and_(OkDataLevel3.name.like(t_name + "%"))).all()
    children_object_list = OkDataLevel3.query.filter(OkDataLevel3.pid.in_([str(item.id) for item in res_object_list]))
    # children_list = [item.to_dict() for item in children_object_list]
    id2children = {}

    for item in children_object_list:
        if id2children.get(item.pid):
            id2children.get(item.pid).append(item.to_dict())
        else:
            id2children[item.pid] = [item.to_dict()]
    res_list = []
    for item in res_object_list:
        temp_dict = {
            "id": item.id,
            "pid": item.pid,
            "name": item.name,
            "ext_name": item.ext_name,
            "children_list": id2children.get(str(item.id))
        }
        res_list.append(temp_dict)
    res_dict["data"] = res_list
    return jsonify(res_dict)


# 测试
@blueprint.route("/get_location_from_deep", methods=["GET"])
def get_location_from_level():
    params_dict = request.args
    t_deep = params_dict.get("deep", "1")
    res_dict = {
        "code": 200,
        "msg": "success"
    }
    res_objects = OkDataLevel4.query.filter(and_(OkDataLevel4.deep == t_deep)).all()
    res_dict["data"] = [item.to_dict() for item in res_objects]
    return jsonify(res_dict)


import re


@blueprint.route("/get_phone_location", methods=["GET"])
def get_phone_location():
    params_dict = request.args
    t_phone = params_dict.get("phone", "15827320000")
    res_dict = {
        "code": 200,
        "msg": "success",
        "data": {}
    }
    phone_re = re.compile(r'(\d{3})\D*(\d{3})\D*(\d{4})\D*(\d*)$', re.VERBOSE)
    if re.match(phone_re, t_phone):
        t_phone_section = t_phone[:7]
        res_one = TbPhoneSegmentInfo.query.filter(TbPhoneSegmentInfo.section == t_phone_section).first()
        if res_one:
            res_dict["data"] = res_one.to_dict()
        else:
            res_dict["data"] = {}
        return jsonify(res_dict)
    else:
        res_dict["code"] = 500
        res_dict["msg"] = "电话号码格式不正确"
        return jsonify(res_dict)

# api.add_resource(HelloWorldCrud, '/<string:todo_id>')
